#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Author:liang futing
#@Time :2019/4/4 11:34
from django.shortcuts import render,HttpResponse,reverse,redirect,get_object_or_404
from web.models import Customer,Payment
from web.forms.payment import PaymentForm,PaymentUserForm

def payment_list(request):
    data_list=Payment.objects.all()
    return render(request,'payment_list.html',{'data_list':data_list})

def payment_add(request):
    if request.method=='GET':
        form=PaymentForm()
        return render(request,'payment_add.html',{'form':form})
    form=PaymentForm(data=request.POST)
    if form.is_valid():
        form.save()
        return redirect(reverse('web:payment_list'))
    return render(request,'payment_add.html',{'form':form})

def payment_edit(request,pk):
    obj=get_object_or_404(Payment,pk=pk)
    if request.method=='GET':
        form=PaymentForm(instance=obj)
        return render(request,'payment_add.html',{'form':form})
    form=PaymentForm(data=request.POST,instance=obj)
    if form.is_valid():
        form.save()
        return redirect(reverse('web:payment_list'))
    return render(request,'payment_add.html',{'form':form})

def payment_del(request,pk):
    Payment.objects.filter(pk=pk).delete()
    return redirect(reverse('payment_list'))

